<?php
/*
* PHP Template created by Nicky
*/

class oper extends spController{
	
	var $pageSize = 20 ;
  
	
	function changestatus(){
		$id = $this->spArgs('id');
		$status = $this->spArgs('status');
		if(empty($id)||empty($status)){
			FN::_Ajax_return(false,'参数有误');
		}
		$oModel = spClass('wx_order');
		$rs = $oModel->update(array('id'=>$id),array('status'=>$status));
		if($rs){
			FN::_Ajax_return(true,'操作完成');
		}else{
			FN::_Ajax_return(false,'操作失败，请稍后重试');
		}
	} 
	
	function orderlist(){
		
		$where = ' 1=1 ';
		$whereStr = '';
		$whereArr = array();
		
		$page = $this->spArgs('page', 1);
		
		$s_username = $this->spArgs('s_username','');
		if(!empty($s_username)){
			$where .=' and username like "'.trim($s_username).'%" ';
			$whereStr .= '/s_username/'.$s_username;
			$whereArr['s_username'] = $s_username;
		} 
		
		$s_phone = $this->spArgs('s_phone','');
		if(!empty($s_phone)){
			$where .=' and phone like "'.trim($s_phone).'%" ';
			$whereStr .= '/s_phone/'.$s_phone;
			$whereArr['s_phone'] = $s_phone;
		} 
		
		$limit = ($page-1)*$this->pageSize.','.$this->pageSize;
			
		$oModel = spClass('wx_order');  
		$sqlstr = 'select * from orderlist where '.$where.' order by created desc limit '.$limit;
		$recordList = $oModel->query($sqlstr);
		
		$sqlstr = 'select count(*) as ct from orderlist where '.$where.' order by created desc ';
		$totalCount = $oModel->query($sqlstr);
		$trow = $totalCount->fetch_assoc();
		$totalCount = $trow['ct'];
		
		$orderList = array();
		while($row = $recordList->fetch_assoc()){ 
			$orderList[] = $row;
		}
		
		$totalPage = ceil($totalCount/$this->pageSize);
		$cPage = $page;
		
		$pageList = array();
		for($tpIndex=$cPage-3;$tpIndex<=$cPage+3;$tpIndex++){
			if($tpIndex>0&&$tpIndex<=$totalPage){
				$pageList[] = $tpIndex;
			}
		}
		
		$pager = array(
			'totalPgae' => $totalPage,
			'totalCount'	=>	$totalCount,
			'pageSize'	=>	$this->pageSize,
			'pageList' =>	$pageList,
			'cPage' => $cPage,
			'parm' => $whereStr,
		);
		$this->orderList = $orderList;
		$this->pager = $pager;
		$this->where = $where;
		$this->wherestr = $whereStr;
		$this->whereArray = $whereArr; 
		 
	}
	
	function export(){
		$ST = array(
			1=>'未联系',
			2=>'已联系未报名',
			3=>'确认报名'
		);
		try{
			$oModel = spClass('wx_order');
			$list = $oModel->findAll();
			$filename = '/public/upload/'.date('Ymd_His',time()).'_'.mt_rand(1000,9999).'.csv';
			$f = fopen('.'.$filename,'a+');
			foreach($list as $line){
				$line['status'] =  $ST[$line['status']]; 
				fwrite($f,implode(',',$line).'
'); 
			}
			fclose($f);
			FN::_Ajax_return(true,'操作完成',$filename); 
		} catch (Exception $ex) {
			FN::_Ajax_return(false,'导出失败,请稍后重试',$filename); 
		}
			
	}
	
	function __construct(){
		parent::__construct();
		$UID = $_SESSION['LOGIN'];
		if(empty($UID) || strlen($UID)<1){
			//echo '<script>window.top.location="/admin.php?c=main&a=login";</script>';
			exit();
		}
	}
}